package com.software.levelupandroid.home.ui.todayTask;

import android.content.Context;
import android.os.Looper;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.Result;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.CallBack;
import com.software.levelupandroid.home.ui.home.HomeRepository;

import java.util.List;

public class TodayTaskViewModel extends ViewModel implements CallBack<List<Task>> {

    private final MutableLiveData<Resource<List<Task>>> taskListLiveData;
    private final MutableLiveData<Resource<Result>> isUpdateLiveData;

    public TodayTaskViewModel() {
        taskListLiveData = new MutableLiveData<>();
        isUpdateLiveData = new MutableLiveData<>();
    }

    public LiveData<Resource<List<Task>>> getTaskListLiveData() {
       return taskListLiveData;
    }

    public LiveData<Resource<Result>> getIsUpdateLiveData() {
        return isUpdateLiveData;
    }

    /**
     * 获取指定日期下的所有任务
     * @param context
     * @param date
     */
    public void getTasksByDate(Context context, String date){
        TodayTaskRepository.getTodayTaskRepository()
                .getTasksByDateFromServer(this, context, date);
    }

    /**
     * 是否完成任务
     * @param task
     * @param context
     */
    public void isFinishTask(Task task, Context context){
        TodayTaskRepository.getTodayTaskRepository().isFinishTask(task, new CallBack<Task>() {
            @Override
            public void onSuccess(Task data) {
                Resource<List<Task>> resource = taskListLiveData.getValue();
                List<Task> tasks = resource.getData();
                if (tasks != null){
                    int index = tasks.indexOf(data);
                    if (index >=0 ){
                        tasks.set(index, data);
                        resource.onSuccess(tasks);
                        runOnMainThread(taskListLiveData, resource);
                    }else {
                        resource.onFailure("任务索引小于0", tasks);
                        runOnMainThread(taskListLiveData, resource);
                    }
                }else {
                    resource.onFailure("未获取到任务", tasks);
                    runOnMainThread(taskListLiveData, resource);
                }
            }

            @Override
            public void onFailure(String msg) {
                Resource<List<Task>> resource = taskListLiveData.getValue();
                List<Task> data = resource.getData();
                resource.onFailure(msg, data);
                runOnMainThread(taskListLiveData, resource);
            }
        }, context);
    }

    /**
     * 删除任务
     * @param task
     * @param context
     */
    public void deleteTask(Task task, Context context){
        TodayTaskRepository.getTodayTaskRepository().deleteTask(task, new CallBack() {
            @Override
            public void onSuccess(Object data) {
                Resource<List<Task>> resource = taskListLiveData.getValue();
                List<Task> tasks = resource.getData();
                if (tasks != null){
                    int index = tasks.indexOf(task);
                    if (index >= 0){
                        tasks.remove(index);
                        resource.onSuccess(tasks);
                        runOnMainThread(taskListLiveData, resource);
                    }else {
                        resource.onFailure("任务索引小于0", tasks);
                        runOnMainThread(taskListLiveData, resource);
                    }
                }else {
                    resource.onFailure("未获取到任务", tasks);
                    runOnMainThread(taskListLiveData, resource);
                }
            }

            @Override
            public void onFailure(String msg) {
                Resource<List<Task>> resource = taskListLiveData.getValue();
                List<Task> data = resource.getData();
                resource.onFailure(msg, data);
                runOnMainThread(taskListLiveData, resource);
            }
        }, context);
    }


    /**
     * 更新用户的经验值
     * @param experience
     * @param context
     */
    public void updateExperience(double experience, Context context){
        HomeRepository.getHomeRepository().updateExperience(experience, context, new CallBack() {
            @Override
            public void onSuccess(Object data) {
                Result result = (Result) data;
                updateRunOnMainThread(isUpdateLiveData, Resource.success(result));
            }

            @Override
            public void onFailure(String msg) {
                updateRunOnMainThread(isUpdateLiveData, Resource.failure(msg, null));
            }
        });
    }

    @Override
    public void onSuccess(List<Task> data) {
        runOnMainThread(taskListLiveData, Resource.success(data));
    }

    @Override
    public void onFailure(String msg) {
        Log.e("todayTask", msg);
        runOnMainThread(taskListLiveData, Resource.failure(msg, null));
    }

    /**
     * 将LiveData的赋值放在主线程上
     * @param taskListLiveData
     * @param data
     */
    private void runOnMainThread(MutableLiveData<Resource<List<Task>>> taskListLiveData, Resource<List<Task>> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            taskListLiveData.setValue(data);
        else
            //当前线程不在主线程
            taskListLiveData.postValue(data);
    }

    /**
     * 将isUpdateLiveData的赋值放在主线程上
     * @param isUpdateLiveData
     * @param data
     */
    private void updateRunOnMainThread(MutableLiveData<Resource<Result>> isUpdateLiveData, Resource<Result> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            isUpdateLiveData.setValue(data);
        else
            //当前线程不在主线程
            isUpdateLiveData.postValue(data);
    }
}